﻿<?php
	include '../inc/conn.php';
	include '../sendmail.php';

	session_start();
	extract($_POST);
	extract($_GET);
	$pagestr = end(explode("/",$_SERVER['PHP_SELF']));
	unset($_POST,$_GET);

	if (isset($act))
	{
		if ($act == 'add' && isset($_SESSION['logname']))
		{
			if (empty($partnum) || empty($testdesc)){
				echo "<script>alert('料号或描述不能为空!!');window.location='$pagestr';</script>";
				die("料号或描述不能为空!");
			}

			$partnum=trim(strtoupper($partnum));
			$testdesc=trim(strtoupper($testdesc));
			if (strlen($partnum)<=5){
				echo "<script>alert('料号长度不对！');window.location='$pagestr';</script>";
				die("料号长度不对!");
			}

			if (strlen($testdesc)<1){
				echo "<script>alert('描述长度不对！');window.location='$pagestr';</script>";
				die("描述长度不对！");
			}

			if(isset($isEnable))
				$isEnable=1;
			else
				$isEnable=0;

			$db = new mysql();
			$db->query("select * from test_partnum where PartNum = '${partnum}'");
			$num = $db->db_num_rows();
			if($num == 0){
				$db->query("INSERT INTO `test_partnum` (`PartNum`,`TestCode`) values ('${partnum}', '${testdesc}')");
				// //SendMail("增加[$sku] Sound Blaster SSID", "<b>机种：</b><font color='blue'>$sku</font></br><b>SSID：</b><font color='blue'>$ssid</font></br>需要确认是否正确！", "TE");
				// $param = array(
					// 'aSync'=>'Yes',
					// 'title'=>"增加[$sku] Sound Blaster SSID",
					// 'msg'=>"<b>机种：</b><font color='blue'>$sku</font></br><b>SSID：</b><font color='blue'>$ssid</font></br>需要确认是否正确！",
					// 'to'=>'TE'
				// );
				// doAsyncMail($param);
				$db->close();
				echo "<script>alert('数据添加成功!');window.location.href='/admin/admin_testCode.php'</script>";
			}else{
				$db->close();
				echo "<script>alert('料号已存在，请不要重复添加!');window.location.href='/admin/admin_testCode.php'</script>";
			}
		}

		if ($act == 'change' && isset($_SESSION['logname']))
		{
			if (empty($partnum) || empty($testdesc)){
				echo "<script>alert('机种或SSID值不能为空!!');window.location='$pagestr';</script>";
				die("机种或SSID值不能为空!");
			}

			$partnum=trim(strtoupper($partnum));
			$testdesc=trim(strtoupper($testdesc));
			if (strlen($partnum)<=8){
				echo "<script>alert('料号长度不对！');window.location='$pagestr';</script>";
				die("料号长度不对!");
			}

			if (strlen($testdesc)<1){
				echo "<script>alert('描述长度不对！');window.location='$pagestr';</script>";
				die("描述长度不对！");
			}

			if(isset($isEnable))
				$isEnable=1;
			else
				$isEnable=0;

			$db = new mysql();
			$table = "test_partnum";
			$condition = "ID=${id}";
			$mod_content="`PartNum`='${partnum}', `TestCode`='${testdesc}', `IsEnable`=$isEnable";
			$db->update($table, $mod_content, $condition);
			$db->close();
			echo "<script>alert('内容修改完成!');window.location.href='$pagestr';</script>";

		}
	}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script type="text/javascript" src="/js/jquery.min.js"></script>
<script type="text/javascript" src="/js/jquery-migrate.min.js"></script>
<script type="text/javascript" src="/xhEditor/xheditor.min.js"></script>
<script type="text/javascript" src="/xhEditor/xheditor_lang/zh-cn.js"></script>
<script type="text/javascript" src="/js/bootstrap.min.js"></script>
<script>
	$(function(){
		$("#ADDDIV").hide();
		$("#ADDPN").click(function(){
			$("#ADDDIV").toggle();
		});

		$("#addbtn").click(function(){
			if ($("#partnum").val() == ""){
				alert("机种名没有填写！");
				return false;
			}

			if ($("#testdesc").val() == ""){
				alert("SSID没有填写！");
				return false;
			}			
		});
	});
</script>
<title>测试代码描述管理表</title>
</head>
<body class="background">
<div class="container-fluid white-background pagecontent">
<?
	if(isset($_SESSION['logname'])){
		if ($pagename=="changeitem"){
			echo "<label>操作项次: </label><a id='ADDPN' href='#'> <span class=\"glyphicon glyphicon-plus\"></span>添加</a> | <a id='ADDDUTY' href='admin_testCode.php'> <span class=\"glyphicon glyphicon-backward\"></span>返回</a>";
		}else{
			echo "<label>操作项次: </label><a id='ADDPN' href='#'> <span class=\"glyphicon glyphicon-plus\"></span>添加</a>";
		}
	}
	echo "<div id='ADDDIV'>\n";
	echo "<form action='$pagestr?act=add' method='post'>\n";
	echo "	<div>\n";
	echo "	添加内容:<br />\n";
	echo "		料号 : <input name='partnum' id='partnum'><br>\n";
	echo "		描述 : <input name='testdesc' id='testdesc'><br>\n";
	//echo "		启用 : <input type='checkbox' name='isEnable' value='1' />\n";
	echo "	</div>\n";
	echo "	<div>\n<input class='btn btn-success btn-primary' name='submit' value='提交' type='submit' id='addbtn'>\n<input class='btn btn-danger' name='reset' value='重写' type='reset'>\n</div>\n";
	echo "</form>\n";
	echo "</div>\n";

	// change item page
	if ($pagename=="changeitem"){
		$db = new mysql();
		$sql="select * from test_partnum where ID=$id";
		$db->query($sql);
		$num = $db->db_num_rows();
		$row = $db->fetch_assoc();
		echo "\n<form action='$pagestr?act=change&id=${row['ID']}' method='post'>\n";
		echo "	<div>\n";
		echo "	修改内容:<br />\n";
		echo "		料号 :<input name='partnum' id='partnum' value='${row['PartNum']}'><br>";
		echo "		描述 :<input name='testdesc' id='testdesc' value='${row['TestCode']}'><br>";
		if($row['IsEnable']==true){
			echo "		启用 : <input type='checkbox' name='isEnable' value='1' checked='checked' />";
		}else{
			echo "		启用 : <input type='checkbox' name='isEnable' value='1' />";
		}
		echo "	</div>\n";
		echo "	<div>\n<input class='btn btn-success btn-primary' name='submit' value='提交' type='submit' id='addbtn'>\n";
		echo "  <input class='btn btn-danger' name='reset' value='重写' type='reset'>\n</div>\n";
		echo "</form>\n";
		$db->close();
	}else{
		$db = new mysql();
		if(isset($sortby)){
			switch($sortby){
				case "pn":
					$db->query("select * from test_partnum order by PartNum asc");
					break;
				case "tc":
					$db->query("select * from test_partnum order by TestCode asc");
					break;
			}
		}else{
			$db->query("select * from test_partnum order by add_date desc");
		}
		$num = $db->db_num_rows();
		echo "<table border=0 cellpadding=0 class='table table-striped'>";
		echo "<tr class='success'><th>Item</th><th><a href='$pagestr?sortby=pn'>料号</a></th><th><a href='$pagestr?sortby=tc'>测试描述</a></th>
				<th>启用状态</th><th>后台处理状态</th><th>添加时间</th><th>更新时间</th><th>操作</th></tr>";
		for($i=0; $i<$num; $i++){
			$row = $db->fetch_assoc();
			$item = $i + 1;
			echo "<tr>";
			echo "<td>$item</td>";
			if(substr($row['add_date'],0,10)==date("Y-m-d")){
				echo "<td><font color='blue'>${row['PartNum']}</font></td>";
				echo "<td><font color='blue'>${row['TestCode']}</font></td>";
			}else{
				echo "<td>${row['PartNum']}</td>";
				echo "<td>${row['TestCode']}</td>";
			}

			if($row['IsEnable']==0){
				echo "<td><font color='red'>停用</font></td>";
			}else{
				echo "<td><font color='blue'>启用</font></td>";
			}

			if($row['Flag']==0){
				echo "<td><font color='red'>已处理</font></td>";
			}else{
				echo "<td><font color='blue'>待处理</font></td>";
			}

			echo "<td>${row['add_date']}</td>";
			echo "<td>${row['up_date']}</td>";
			if(isset($_SESSION['logname'])){
				echo "<td><a href='$pagestr?pagename=changeitem&id=${row['ID']}'><span class=\"glyphicon glyphicon-edit\"></span></a></td>";
			}else{
				echo "<td>#</td>";
			}
			echo "</tr>\n";
		}
		echo "</table>";
		$db->close();
	}
?>
</div>
</body>
</html>
